import csv

import requests
from bs4 import BeautifulSoup
def get_html(url):
    head={'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'}
    try:
        r=requests.get(url)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as error:
        print(error)
def parser(html):
    soup=BeautifulSoup(html,"lxml")
    out_list=[]
    for row in soup.select("div>ul>li>a>article>h1"):
        soup.select("div>ul>li>a>article>h1")
        row_data=[row.text]
        out_list.append(row_data)
    return out_list
def save_csv(path,content):
    with open(path,"w+",encoding="utf-8",newline='')as f:
        csv_write=csv.writer(f)
        csv_write.writerows(content)
if __name__ == '__main__':
     url="http://www.mxbc.com/product"
     html=get_html(url)
     out_list=parser(html)
     path='d://蜜雪冰城.csv'
     save_csv(path,out_list)

